<?php
/*
 * Created on Feb 21, 2009
 * FileName: AjaxController.php
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
	
class Ads_AjaxController extends Clu_Controller_Action_Hvn
{
	public function init()
	{
		$this->m = new ads_Model_Ads();
		parent::init();
	}
    
    public function deleteAdsAction()
	{
		$id = $this->_request->getParam('id'); 
		if ($this->_request->isXmlHttpRequest())
		{
			$ret = array();
			//check permission
		    if (strpos ($sess->userPermissions, 'administer user') === false)
			{
				$ret['success'] = false;
				$ret['message'] = t(414);
				$this->_helper->json->sendJson($ret);
				exit();
			}
			
            if ($this->m->deleteByPrimary ($id) === false)
            {
            	$ret['success'] = false;
            }
            else
            	$ret['success'] = true;

			$this->_helper->json->sendJson($ret);
		}
	}	
	
	
	public function bulkAdsAction()
    {        
        if($this->_request->isXmlHttpRequest())
        {
	    	$places = $this->_request->getParam('places');
        	$ret = array();
        	//check permission
		    if (strpos ($sess->userPermissions, 'administer user') === false)
			{
				$ret['success'] = false;
				$ret['message'] = t(414);
				$this->_helper->json->sendJson($ret);
				exit();
			}
			
	        if (count($places) > 0)
	        {
	            $action = $this->_request->getParam('bulkaction');
		        if ($action === 'delete')
		        {
		        	if( $this->m->deleteListByPrimary($places))
	            		$ret['success'] = true;
		        }
		        else if($action === 'status')
		        {
	                $ret['status'] = $this->_request->getParam('changestatus');
	                if($this->m->bulkUpdateStatus($places, $ret))
	                	$ret['success'] = true;
		        }
	        }
	        else
	            $ret['success'] = false;
	        $ret['ids'] = $places;
	        $ret['bulkaction'] = $action;
	        $this->_helper->json->sendJson($ret);
        }
    }
    
    public function statusDeletedAction()
    {
    	$placeId = $this->_request->getParam('id');
    	if($this->_request->isXmlHttpRequest())
    	{
    		$ret = array();
    		//check permission
		    if (strpos ($sess->userPermissions, 'administer user') === false)
			{
				$ret['success'] = false;
				$ret['message'] = t(414);
				$this->_helper->json->sendJson($ret);
				exit();
			}
			
    		$ret['status'] = 'deleted';
    		if($this->m->bulkUpdateStatus((array)$placeId, $ret))
	        	$ret['success'] = true;
	    	$this->_helper->json->sendJson($ret);
    	}
    }
    
}
?>
